package com.yjd.bi.user.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import com.yjd.bi.user.dao.IInvestRateDaysDao;
import com.yjd.bi.user.model.InvestRateDaysModel;
import com.yjd.bi.user.service.IInvestRateDaysService;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;

/**
 * <b>description</b>：用户投资项目日留存率业务实现<br>
 * <b>time</b>：2016-04-27 17:12:31 <br>
 * <b>author</b>： renbangjie renbangjie@126.com
 */
@Service("investRateDaysService")
public class InvestRateDaysServiceImpl extends BaseServiceImpl implements
		IInvestRateDaysService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public InvestRateDaysModel insert(InvestRateDaysModel model)
			throws Exception {
		return this.investRateDaysDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(InvestRateDaysModel model) throws Exception {
		return this.investRateDaysDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param statistics_date
	 * @return
	 * @throws Exception
	 */
	public int delete(long statistics_date) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("statistics_date", statistics_date);
		return this.investRateDaysDao.deleteModel(paramMap, true);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<Integer, List<Integer>> getInvestDaysRate(Integer skip)
			throws Exception {
		Date currentDate = DateUtils
				.truncate(new Date(), Calendar.DAY_OF_MONTH);
		List<InvestRateDaysModel> list = super.getModelList(FrameUtil
				.newHashMap("start", DateUtil.format(
						DateUtils.addDays(currentDate, -skip),
						DateUtil.PATTERN_yyyyMMdd), "end", DateUtil.format(
						currentDate, DateUtil.PATTERN_yyyyMMdd), "sort",
						"`statistics_date`"), DbWREnums.READ);
		Map<Integer, List<Integer>> resultMap = new HashMap<Integer, List<Integer>>();
		for (InvestRateDaysModel investRateDaysModel : list) {
			if (!resultMap
					.containsKey(investRateDaysModel.getStatistics_date())) {
				resultMap.put(investRateDaysModel.getStatistics_date(),
						new ArrayList<Integer>());
				resultMap.get(investRateDaysModel.getStatistics_date()).add(
						investRateDaysModel.getInvest_count());
			}
			if (resultMap.get(investRateDaysModel.getStatistics_date()).size() > skip) {
				continue;
			}
			resultMap.get(investRateDaysModel.getStatistics_date()).add(
					investRateDaysModel.getRate());
		}
		return resultMap;
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	public List<List<Integer>> getInvestDaysRate(Integer skip,
			Long condition) throws Exception {

		Date currentDate = DateUtils
				.truncate(new Date(), Calendar.DAY_OF_MONTH);
		List<InvestRateDaysModel> list = super.getModelList(FrameUtil
				.newHashMap("start", DateUtil.format(
						DateUtils.addDays(currentDate, -skip),
						DateUtil.PATTERN_yyyyMMdd), "end", DateUtil.format(
						currentDate, DateUtil.PATTERN_yyyyMMdd), "sort",
						"`statistics_date` desc, `invest_date`", "condition", condition),
				DbWREnums.READ);

		Map<Integer, List<Integer>> dataMap = new HashMap<Integer, List<Integer>>();
		for (InvestRateDaysModel investRateDaysModel : list) {
			if (!dataMap.containsKey(investRateDaysModel.getStatistics_date())) {
				dataMap.put(investRateDaysModel.getStatistics_date(),
						new ArrayList<Integer>());
				dataMap.get(investRateDaysModel.getStatistics_date()).add(
						investRateDaysModel.getInvest_count());
			}
			if (dataMap.get(investRateDaysModel.getStatistics_date()).size() > skip) {
				continue;
			}
			dataMap.get(investRateDaysModel.getStatistics_date()).add(
					investRateDaysModel.getRate());
		}

		// 排序
		List<Map.Entry<Integer, List<Integer>>> mapList = new ArrayList<Map.Entry<Integer, List<Integer>>>(
				dataMap.entrySet());
		Collections.sort(mapList,
				new Comparator<Map.Entry<Integer, List<Integer>>>() {
					@Override
					public int compare(Entry<Integer, List<Integer>> o1,
							Entry<Integer, List<Integer>> o2) {
						return o1.getKey().compareTo(o2.getKey());
					}
				});

		// 新的Map
		List<List<Integer>> resultList = new ArrayList<List<Integer>>();
		for (Entry<Integer, List<Integer>> entry : mapList) {
			entry.getValue().add(0, entry.getKey());
			resultList.add(entry.getValue());
		}
		return resultList;

	}

	@Resource
	private IInvestRateDaysDao investRateDaysDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.investRateDaysDao;
	}

}
